package org.jetnuke.modules.content;

import java.util.Iterator;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.PagesDAO;
import org.jetnuke.entities.PagesCategories;

public class ListPages implements IAction {

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {

		Iterator catIterator = DataAccessDriver.getInst().newPagesCatDAO().selectAllDifferentCID(-1).iterator();
		//Iterator catIterator = pages_categoriesPeer.doSelect(
			//	new Criteria().add(pages_categoriesPeer.CID, -1,
				//		Criteria.NOT_EQUAL)).iterator();
		LinkedList ls = new LinkedList();
		PagesDAO pm=DataAccessDriver.getInst().newPagesDAO();
		while (catIterator.hasNext()) {
			PagesCategories cat = (PagesCategories) catIterator.next();

			//Criteria criteria = new Criteria().add(pagesPeer.CID, cat.getCID())
			//		.add(pagesPeer.ACTIVE, 1);
			//int numberOfRecords = pagesPeer.count(criteria);
			
			int numberOfRecords = pm.countActiveByCID(cat.getCID());
			
			if (numberOfRecords > 0) {
				ls.add(cat);
				//System.out.println("ho trovato per "+cat.getCID()+ " num:
				// "+numberOfRecords);
			}
		}
		vc.put("rows", ls);
		return new ActionForward(op, info);
	}

}
